package  
{
	import com.phantasy.caches.CacheLoader;
	import com.phantasy.globals.GameParameters;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.utils.setTimeout;

	/**
	 *游戏预加载视图 
	 * @author Liseen
	 * 
	 */	
	public class GameLoading extends Sprite
	{
		private static var _instance:GameLoading;
		
		private var _callback:Function;
		
		[Embed(source="GameLoading_barImage.png")]
		private var GameLoading_barImage:Class;
		[Embed(source="GameLoading_barBgImage.png")]
		private var GameLoading_barBgImage:Class;
		private var tips:Array;
		private var offsetX:int;
		private var offsetY:int;
		private var bgLayer:Sprite;
		private var welcome:Bitmap;
		private var bar:Shape;
		private var barAll:Shape;
		private var itemText:TextField;
		private var itemPercentText:TextField;
		private var barBitmapData:BitmapData;
		private var totalText:TextField;
		private var warnTxt:TextField;
		private var freshText:TextField;
		private var tipTxt:TextField;
		private var f8:TextField;
		private var time:int = 24;
		private var leftTimeText:TextField;
		private var leftTimer:Timer;
		private var bar_bg1:Bitmap;
		private var bar_bg2:Bitmap;
		private var barImage:Class;
		private var barBgImage:Class;
		private var lastPercent:Number = 0;
		
		public function GameLoading(callBackFun:Function)
		{
			this.barImage = GameLoading_barImage;
			this.barBgImage = GameLoading_barBgImage;
			super();
			_callback = callBackFun;
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);
		}
		/**
		 *获取游戏预载视图实例 
		 * @param callBack
		 * @return 
		 * 
		 */		
		public static function getInstance(callBack:Function=null):GameLoading
		{
			if(GameLoading._instance==null){
				GameLoading._instance = new GameLoading(callBack);
			}
			
			return GameLoading._instance;
		}
		
		private function onAddToStageHandle(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddToStageHandle);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemove);
			this.stage.addEventListener(Event.RESIZE, this.onStageResize);
			
			bgLayer = new Sprite();
			this.addChild(bgLayer);
			this.bar = new Shape();
			this.bar.y = 487;
			this.barAll = new Shape();
			this.barAll.y = 511;
			addChild(this.bar);
			addChild(this.barAll);
			
			var tformat:TextFormat = new TextFormat(null, 12, 0xFFFFFF, null, null, null, null, null, "left");
			this.itemText = this.createTextField("", 326, 473, 300, 20, tformat, this);
			this.itemText.filters = [new GlowFilter(0, 1, 2, 2, 20)];
			this.totalText = this.createTextField("", 326, 533, 300, 20, tformat, this);
			this.leftTimeText = this.createTextField("剩余时间：20秒", 600, 533, 150, 20, tformat, this);
			var _local3:Bitmap = new this.barImage();
			this.barBitmapData = _local3.bitmapData;
			var _local4:Bitmap = new this.barBgImage();
			var _local5:BitmapData = _local4.bitmapData;
			this.bar_bg1 = new Bitmap();
			this.bar_bg2 = new Bitmap();
			this.bar_bg1.bitmapData = _local5;
			this.bar_bg2.bitmapData = _local5;
			this.bar_bg1.x = ((stage.stageWidth - this.bar_bg1.width) >> 1);
			this.bar_bg1.y = 487;
			this.bar_bg2.x = this.bar_bg1.x;
			this.bar_bg2.y = 511;
			this.bgLayer.addChild(this.bar_bg1);
			this.bgLayer.addChild(this.bar_bg2);
			this.bar.x = this.bar_bg1.x;
			this.barAll.x = this.bar_bg1.x;
			this.itemText.x = (this.totalText.x = this.bar.x);
			this.leftTimeText.x = (this.totalText.x + 350);
			setTimeout(this.doTxts, 60);
			this.leftTimer = new Timer(1000);
			this.leftTimer.addEventListener(TimerEvent.TIMER, this.onTimer);
			this.leftTimer.start();
			if(_callback!=null){
				_callback();
			}
		}
		
		private function onRemove(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemove);
			this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
		}
		
		private function onStageResize(event:Event):void
		{
			if(stage){
				this.bar_bg1.x = ((stage.stageWidth - this.bar_bg1.width) >> 1);
				this.bar_bg2.x = this.bar_bg1.x;
				this.bar.x = this.bar_bg1.x;
				this.barAll.x = this.bar_bg1.x;
				this.itemText.x = this.bar.x;
				this.totalText.x = this.bar.x;
				this.leftTimeText.x = (this.bar_bg1.x + 392);
				if (this.welcome)
				{
					this.welcome.x = ((stage.stageWidth - this.welcome.width) >> (1 + this.offsetX));
				}
				if (this.warnTxt && this.freshText &&this.tipTxt)
				{
					this.warnTxt.x = ((stage.stageWidth - this.warnTxt.width) >> 1);
					this.warnTxt.y = (stage.stageHeight - 24);
					this.freshText.x = ((stage.stageWidth - this.freshText.width) >> 1);
					this.freshText.y = (this.warnTxt.y - 22);
					this.tipTxt.x = ((stage.stageWidth - this.tipTxt.width) >> 1);
					this.tipTxt.y = (this.freshText.y - 22);
				}
			}
		}
		
		private function createTextField(_arg1:String, _arg2:Number, _arg3:Number, _arg4:Number, _arg5:Number, _arg6:TextFormat, _arg7:DisplayObjectContainer):TextField
		{
			var _local8:TextField = new TextField();
			_local8.defaultTextFormat = _arg6;
			_local8.selectable = false;
			_local8.width = _arg4;
			_local8.height = _arg5;
			_local8.x = _arg2;
			_local8.y = _arg3;
			_local8.htmlText = _arg1;
			_arg7.addChild(_local8);
			return (_local8);
		}
		
		private function doTxts():void
		{
		
		}
		
		private function onTimer(event:TimerEvent):void
		{
			if (this.time > 5)
			{
				this.time--;
				this.leftTimeText.text = (("剩余时间：" + this.time) + "秒");
			}
			else
			{
				this.leftTimer.stop();
				this.leftTimer.removeEventListener(TimerEvent.TIMER, this.onTimer);
				this.leftTimer = null;
			};
		}
		
		
		/**
		 *销毁 
		 * 
		 */		
		public function destroy():void
		{
			if(this.parent){
				this.parent.removeChild(this);
			}
			
			GameLoading._instance=null;
		}
		
		public function loadWelcomeBG():void
		{
			var _local2:String;
			var _local1:String = GameParameters.getInstance().proxyName;
			var _local3:String = GameParameters.getInstance().gameBackgroundPic;
			if ((((_local3 == "")) || ((_local3 == null))))
			{
				_local2 = "assets/loading/bg.jpg";
			}
			else
			{
				_local2 = ("assets/loading/" + _local3);
			}
			var loader:CacheLoader = new CacheLoader();
			//loader.addEventListener(Event.COMPLETE, this.welcomeOK);
			//loader.load(new URLRequest(_local2),true);
		}
		
		public function setItemPercent(_arg1:String, _arg2:Number, _arg3:Number):void
		{
			var _local4:Number = (_arg2 / _arg3);
			if ((((((_local4 > this.lastPercent)) || ((this.lastPercent == 1)))) && (!(isNaN(_local4)))))
			{
				this.lastPercent = _local4;
				this.barControl(this.bar, _local4);
				this.itemText.text = ((_arg1 + int((_local4 * 100))) + "%");
			}
		}
		
		public function resetItemPercent():void
		{
			this.lastPercent = 0;
			this.itemText.text = "当前进度：100%";
		}
		
		public function setTotalPercent(_arg1:Number, _arg2:Number):void
		{
			var _local3:Number = (_arg1 / _arg2);
			this.barControl(this.barAll, (_arg1 / _arg2));
			this.totalText.text = (("总进度：" + int((_local3 * 100))) + "%");
		}
		
		public function set itemLabel(_arg1:String):void
		{
			this.itemText.text = _arg1;
		}
		
		private function set totalLabel(_arg1:String):void
		{
			this.totalText.text = _arg1;
		}
		
		private function welcomeOK(event:Event):void
		{
			var loader:CacheLoader = (event.target as CacheLoader);
			this.welcome = (loader.getLoader().content as Bitmap);
			this.welcome.x = ((stage.stageWidth - this.welcome.width) >> (1 + this.offsetX));
			this.welcome.y = 15;
			this.bgLayer.addChildAt(this.welcome, 0);
		}
		
		private function barControl(_arg1:Shape, _arg2:Number):void
		{
			_arg1.graphics.clear();
			_arg1.graphics.beginBitmapFill(this.barBitmapData);
			_arg1.graphics.drawRect(0, 0, int((_arg2 * this.barBitmapData.width)), 30);
			_arg1.graphics.endFill();
		}
	}
}